package top.melody.ecommerce.platform.service.basedata.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.melody.ecommerce.common.util.page.PageRows;
import top.melody.ecommerce.common.util.page.Pagination;
import top.melody.ecommerce.platform.EcommerceException;
import top.melody.ecommerce.platform.api.domain.basedata.Region;
import top.melody.ecommerce.platform.api.service.BasedataService;
import top.melody.ecommerce.platform.dal.basedata.RegionMapper;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * CreateUser: renhuan
 * Email:richiedryday@gmail.com
 * CreateTime: 2015/1/28 16:11
 * ModifyUser: renhuan
 * ModifyTime: 2015/1/28 16:11
 * Class Description:
 * To change this template use File | Settings | File Templates.
 */
@Service
public class BasedataServiceImpl implements BasedataService {

    private static final Logger logger = LoggerFactory.getLogger(BasedataServiceImpl.class);
    @Resource
    private RegionMapper regionMapper;

    @Override
    public int getRegionCount(Region queryEntity) throws EcommerceException {
        try {
            int count = regionMapper.getCount(queryEntity);
            return count;
        } catch (Exception e) {
            throw new EcommerceException("execute mapper get region count exception", e);
        }
    }

    @Override
    public PageRows<Region> queryRegionListByPage(Region queryEntity, Pagination page) throws EcommerceException {
        PageRows<Region> pageRows = new PageRows<Region>();
        try {
            List<Region> regionList = regionMapper.queryByPage(queryEntity, page);
            int count = regionMapper.getCount(queryEntity);
            if (page == null)
                page = new Pagination(count, 1, count);
            else
                page.setTotalRows(count);
            pageRows.setPage(page);
            pageRows.setRows(regionList);
            return pageRows;
        } catch (Exception e) {
            throw new EcommerceException("execute mapper query exception", e);
        }

    }
}
